<template>
	<view class="user-manage-container">
		<!-- 顶部搜索栏 -->
		<view class="search-bar">
			<view class="search-input-wrap">
				<text class="search-icon">🔍</text>
				<input type="text" v-model="searchKeyword" placeholder="搜索用户名/手机号" class="search-input" @confirm="handleSearch" />
			</view>
			<button class="search-btn" @click="handleSearch">搜索</button>
		</view>
		
		<!-- 用户列表 -->
		<view class="user-list">
			<view class="list-header">
				<text class="header-cell id-cell">ID</text>
				<text class="header-cell name-cell">用户信息</text>
				<text class="header-cell contact-cell">联系方式</text>
				<text class="header-cell time-cell">时间信息</text>
			</view>
			
			<!-- 用户列表项 -->
			<view class="list-body">
				<view class="list-item" v-for="(user, index) in userList" :key="user.id" @click="viewUserDetail(user.id)">
					<text class="item-cell id-cell">{{ user.id }}</text>
					
					<view class="item-cell info-cell">
						<view class="user-info-row">
							<image :src="user.avatar" class="user-avatar" mode="aspectFill"></image>
							<view class="user-info-content">
								<view class="user-primary-row">
									<text class="user-name">{{ user.username || '未设置' }}</text>
								</view>
								<view class="user-secondary-row">
									<text class="user-role">{{ user.role === 'admin' ? '管理员' : '普通用户' }}</text>
								</view>
							</view>
						</view>
					</view>
					
					<view class="item-cell contact-cell">
						<view class="contact-info">
							<view class="contact-row">
								<text class="contact-label">手机:</text>
								<text class="contact-value">{{ user.phone || '未绑定' }}</text>
							</view>
							<view class="contact-row">
								<text class="contact-label">微信:</text>
								<text class="contact-value">{{ user.wechatId || '未绑定' }}</text>
							</view>
						</view>
					</view>
					
					<view class="item-cell time-cell">
						<view class="time-info">
							<view class="time-row">
								<text class="time-label">注册:</text>
								<text class="time-value">{{ formatDate(user.createTime) }}</text>
							</view>
							<view class="time-row">
								<text class="time-label">登录:</text>
								<text class="time-value">{{ formatDate(user.lastLoginTime) }}</text>
							</view>
						</view>
					</view>
				</view>
				
				<!-- 空状态 -->
				<view class="empty-state" v-if="userList.length === 0">
					<text class="empty-icon">👤</text>
					<text class="empty-text">暂无用户数据</text>
				</view>
			</view>
		</view>
		
		<!-- 分页控件 -->
		<view class="pagination">
			<view class="page-btn" :class="{ disabled: currentPage <= 1 }" @click="prevPage">
				<text class="page-arrow">←</text>
			</view>
			<view class="page-number">{{ currentPage }} / {{ totalPages }}</view>
			<view class="page-btn" :class="{ disabled: currentPage >= totalPages }" @click="nextPage">
				<text class="page-arrow">→</text>
			</view>
		</view>
		
		<!-- 加载状态 -->
		<view class="loading-state" v-if="isLoading">
			<view class="loading-spinner"></view>
			<text class="loading-text">加载中...</text>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import request from '@/utils/request'

// 数据状态
const userList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const totalUsers = ref(0)
const totalPages = computed(() => Math.ceil(totalUsers.value / pageSize.value))
const isLoading = ref(false)
const searchKeyword = ref('')

// 获取用户列表
const fetchUserList = async () => {
	isLoading.value = true
	try {
		const res = await request('/admin/user/list', {
			method: 'GET',
			data: {
				pageNum: currentPage.value,
				keyword: searchKeyword.value
			}
		})
		
		if (res.code === 200) {
			userList.value = res.data.records || []
            console.log(userList.value)
			totalUsers.value = res.data.total || 0
		} else {
			uni.showToast({
				title: res.msg || '获取用户列表失败',
				icon: 'none'
			})
		}
	} catch (error) {
		console.error('获取用户列表失败', error)
		uni.showToast({
			title: '网络错误，请稍后重试',
			icon: 'none'
		})
	} finally {
		isLoading.value = false
	}
}

// 搜索用户
const handleSearch = () => {
	currentPage.value = 1
	fetchUserList()
}

// 上一页
const prevPage = () => {
	if (currentPage.value > 1) {
		currentPage.value--
		fetchUserList()
	}
}

// 下一页
const nextPage = () => {
	if (currentPage.value < totalPages.value) {
		currentPage.value++
		fetchUserList()
	}
}

// 查看用户详情
const viewUserDetail = (userId) => {
	uni.navigateTo({
		url: `/pages/admin/user-detail?id=${userId}`
	})
}

// 编辑用户
const editUser = (userId) => {
	uni.navigateTo({
		url: `/pages/admin/user-edit?id=${userId}`
	})
}

// 格式化日期
const formatDate = (dateString) => {
	if (!dateString) return '未知'
	const date = new Date(dateString)
	return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 格式化时间
const formatTime = (dateString) => {
	if (!dateString) return '未知'
	const date = new Date(dateString)
	return `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
}

// 页面加载时获取用户列表
onMounted(() => {
	fetchUserList()
})
</script>

<style lang="scss" scoped>
.user-manage-container {
	min-height: 100vh;
	padding: 30rpx;
	background: linear-gradient(135deg, #18122B 0%, #120C1C 100%);
	color: #fff;
}

// 搜索栏
.search-bar {
	display: flex;
	margin-bottom: 30rpx;
	gap: 20rpx;
}

.search-input-wrap {
	flex: 1;
	background: #221A36;
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	padding: 0 20rpx;
	border: 1px solid #2D1457;
}

.search-icon {
	font-size: 32rpx;
	margin-right: 10rpx;
	color: #A259FF;
}

.search-input {
	flex: 1;
	height: 80rpx;
	font-size: 28rpx;
	color: #fff;
	background: transparent;
}

.search-btn {
	width: 160rpx;
	height: 80rpx;
	background: linear-gradient(45deg, #7B2FF2, #10AEFF);
	color: #fff;
	font-size: 28rpx;
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border: none;
}

// 用户列表
.user-list {
	background: linear-gradient(135deg, #221A36 0%, #18122B 100%);
	border-radius: 16rpx;
	overflow: hidden;
	box-shadow: 0 4rpx 16rpx #7B2FF244;
	border: 1px solid #2D1457;
	margin-bottom: 30rpx;
}

.list-header {
	display: flex;
	padding: 20rpx;
	background: rgba(162, 89, 255, 0.1);
	border-bottom: 1px solid #2D1457;
}

.header-cell {
	font-size: 28rpx;
	font-weight: bold;
	color: #A259FF;
}

.list-body {
	min-height: 200rpx;
}

.list-item {
	display: flex;
	padding: 20rpx;
	border-bottom: 1px solid #2D1457;
	transition: background 0.2s;
}

.list-item:hover {
	background: rgba(123, 47, 242, 0.05);
}

.list-item:last-child {
	border-bottom: none;
}

.item-cell {
	font-size: 28rpx;
	color: #fff;
	display: flex;
	align-items: center;
}

.id-cell {
	width: 60rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.info-cell {
	width: 310rpx;
}

.user-info-row {
	display: flex;
	align-items: center;
}

.user-avatar {
	width: 70rpx;
	height: 70rpx;
	border-radius: 35rpx;
	margin-right: 16rpx;
	background: #18122B;
	border: 3px solid #54299e;
}

.user-info-content {
	flex: 1;
	display: flex;
	flex-direction: column;
}

.user-primary-row {
	margin-bottom: 8rpx;
}

.user-name {
	font-size: 28rpx;
	font-weight: bold;
	color: #fff;
}

.user-secondary-row {
	display: flex;
	align-items: center;
}

.user-role {
	font-size: 22rpx;
	background: rgba(123, 47, 242, 0.2);
	color: #A259FF;
	padding: 4rpx 12rpx;
	border-radius: 20rpx;
}

.name-cell {
	width: 320rpx;
}

.contact-cell {
	width: 300rpx;
}

.contact-info {
	display: flex;
	flex-direction: column;
	gap: 10rpx;
}

.contact-row {
	display: flex;
}

.contact-label {
	font-size: 24rpx;
	color: #B8B8D1;
	width: 80rpx;
}

.contact-value {
	font-size: 24rpx;
	color: #fff;
	flex: 1;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.time-cell {
	flex: 1;
}

.time-info {
	display: flex;
	flex-direction: column;
	gap: 10rpx;
}

.time-row {
	display: flex;
}

.time-label {
	font-size: 24rpx;
	color: #B8B8D1;
	width: 80rpx;
}

.time-value {
	font-size: 24rpx;
	color: #fff;
	flex: 1;
}

.action-cell {
	width: 160rpx;
	display: flex;
	justify-content: flex-end;
	gap: 10rpx;
}

.action-btn {
	padding: 6rpx 16rpx;
	border-radius: 8rpx;
	font-size: 24rpx;
}

.view-btn {
	background: rgba(162, 89, 255, 0.2);
	color: #A259FF;
}

.edit-btn {
	background: rgba(16, 174, 255, 0.2);
	color: #10AEFF;
}

// 分页控件
.pagination {
	display: flex;
	justify-content: center;
	align-items: center;
	margin-top: 30rpx;
}

.page-btn {
	width: 80rpx;
	height: 80rpx;
	background: #221A36;
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 0 20rpx;
	border: 1px solid #2D1457;
}

.page-btn.disabled {
	opacity: 0.5;
	pointer-events: none;
}

.page-arrow {
	font-size: 32rpx;
	color: #A259FF;
}

.page-number {
	font-size: 28rpx;
	color: #B8B8D1;
}

// 空状态
.empty-state {
	padding: 80rpx 0;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.empty-icon {
	font-size: 80rpx;
	margin-bottom: 20rpx;
	color: #2D1457;
}

.empty-text {
	font-size: 28rpx;
	color: #B8B8D1;
}

// 加载状态
.loading-state {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: rgba(24, 18, 43, 0.7);
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	z-index: 999;
}

.loading-spinner {
	width: 80rpx;
	height: 80rpx;
	border: 6rpx solid #2D1457;
	border-top: 6rpx solid #A259FF;
	border-radius: 50%;
	animation: spin 1s linear infinite;
	margin-bottom: 20rpx;
}

.loading-text {
	font-size: 28rpx;
	color: #B8B8D1;
}

@keyframes spin {
	0% { transform: rotate(0deg); }
	100% { transform: rotate(360deg); }
}

// 响应式布局
@media screen and (max-width: 768px) {
	.time-cell {
		display: none;
	}
	
	.contact-cell {
		width: 220rpx;
	}
}
</style>
